from django.forms.models import model_to_dict
from BloodSpiderAPI import models
from django.db.models import Q  # 必须导入 Q 对象

def utils_model_to_dict(model, *args, **kwargs):
    data = model_to_dict(model)
    data["feedback_id"] = str(model.feedback_id)
    data["status"] = {
        "code": model.status,
        "message": model.get_status_display(),
    }
    data["feedback_type"] = {
        "code": model.feedback_type,
        "message": model.get_feedback_type_display(),
    }
    data["handler"] = {
        "username": model.handler.username if model.handler else None,
        "role": model.handler.role if model.handler else None,
    }
    data["user"] = {
        "username": model.user.username,
        "role": model.user.role,
    }
    data["images"] = []
    for image in models.FeedbackPhoto.objects.filter(feedback=model):
        item = {
            "url": image.photo.url,
            "photo_type": {
                "code": image.photo_type,
                "message": image.get_photo_type_display(),
            }
        }
        data["images"].append(item)
    data["create_time"] = model.create_time.strftime("%Y-%m-%d %H:%M:%S")
    data["update_time"] = model.update_time.strftime("%Y-%m-%d %H:%M:%S")


    return data

